#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
struct student {
	char name[100];
	int jinbu;
	int jieti;
}a[100], c;
int main()
{
	int n, t;
	scanf("%d", &n);
	while (n--)
	{
		scanf("%d", &t);
		int i, j;
		for (i = 0; i < t; i++)
		{
			scanf("%s %d %d", a[i].name, &a[i].jinbu, &a[i].jieti);
		}
		for (i = 0; i < t - 1; i++)
			for (j = 0; j < t - i - 1; j++)
			{
				if (a[j].jinbu < a[j + 1].jinbu)
				{
					c = a[j];
					a[j] = a[j + 1];
					a[j + 1] = c;
				}
			}
		for (i = 0; i < t - 1; i++)
		{
			if (a[i].jinbu == a[i + 1].jinbu)
			{
				if (a[i + 1].jieti > a[i].jieti)
				{
					c = a[i];
					a[i] = a[i + 1];
					a[i + 1] = c;
				}
			}
		}
		for (i = 0; i < t - 1; i++)
		{
			if (a[i].jinbu == a[i + 1].jinbu && a[i].jieti == a[i + 1].jieti)
			{
				if (a[i].name < a[i + 1].name)
				{
					c = a[i];
					a[i] = a[i + 1];
					a[i + 1] = c;
				}
			}
		}
		int count = 1, sum = 0;
		for (i = 0; i < t; i++)
		{
			if (a[i].jinbu == a[i + 1].jinbu && a[i].jieti == a[i + 1].jieti)
			{
				printf("%d %s %d %d\n", count, a[i].name, a[i].jinbu, a[i].jieti);
				sum++;
			}
			else
			{
				printf("%d %s %d %d\n", count, a[i].name, a[i].jinbu, a[i].jieti);
				count++;
				count += sum;
				sum = 0;
			}
		}
	}
	return 0;
}
